//
//  AcceptTermsView.m
//  pa
//
//  Created by HaiJiao Chen on 12-7-30.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "AcceptTermsView.h"
#import "PaintingView.h"
#import "AppDelegate.h"
#import "TicketList.h"
#import "MainObject.h"
#import "FilePack.h"
#import "RejectView.h"
#import "nibLoader.h"
#include "text_def.h"

@implementation AcceptTermsView
@synthesize terms;
@synthesize signature;
@synthesize accept;
@synthesize name;
@synthesize magicView;
@synthesize lastSign;

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        signatures = [[NSMutableArray alloc] initWithCapacity:10];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];

    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];

    [signatures release];
    [super dealloc];
}
- (IBAction)clear:(id)sender
{
    [signature erase];
}
- (IBAction)addSignature:(id)sender
{
    UIImage* temp = [signature saveImageFromGLView];
    if (lastSign.hidden == NO)
    {
        lastSign.hidden = YES;
    }
    else if (temp)
    {
        [signatures addObject:temp];
    }
    else
    {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:[FilePack getStringByID:STR_SIGNATURE_TOO_SIMPLE] delegate:nil cancelButtonTitle:[FilePack getStringByID:STR_OK] otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
    [signature erase];
}
- (IBAction)back:(id)sender
{
    [self removeFromSuperview];
}
- (IBAction)received:(id)sender
{
    UIImage* temp = [signature saveImageFromGLView];
    if (temp)
    {
        [signatures addObject:temp];
    }
    
    if ([signatures count] == 0)
    {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:[FilePack getStringByID:STR_WITHOUT_SIGNATURE] delegate:nil cancelButtonTitle:[FilePack getStringByID:STR_OK] otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
    else if (name.text.length == 0)
    {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:[FilePack getStringByID:STR_WITHOUT_NAME] delegate:nil cancelButtonTitle:[FilePack getStringByID:STR_OK] otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
    else if (bAccept == NO)
    {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:[FilePack getStringByID:STR_WITHOUT_ACCEPT_TERMS] delegate:nil cancelButtonTitle:[FilePack getStringByID:STR_OK] otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
    else
    {
        NSMutableDictionary* dic = [AppDelegate sharedInstance].main.ticketsDataSource.memoryObject;
        dic = [dic valueForKey:[NSString stringWithFormat:@"%lld", ticketID]];
        [dic setValue:[NSNumber numberWithBool:bAccept] forKey:ACCEPT_TERMS];
        [dic setValue:name.text forKey:SIGN_NAME];
        [dic setValue:signatures forKey:SIGNATURE];
        [[NSNotificationCenter defaultCenter] postNotificationName:DataSourceChangeNotification object:[AppDelegate sharedInstance].main.ticketsDataSource userInfo:[NSDictionary dictionaryWithObject:[NSArray arrayWithObject:[NSNumber numberWithLongLong:ticketID]] forKey:@"changed"]];
        [self removeFromSuperview];
    }
}
- (void)viewLoadFromNib:(NSNumber*)param
{
    ticketID = [param longLongValue];
    
    
    [signature setBrushColorWithRed:0 green:0 blue:0];
    terms.text = [AppDelegate sharedInstance].main.term;
    NSMutableDictionary* dic = [AppDelegate sharedInstance].main.ticketsDataSource.memoryObject;
    dic = [dic valueForKey:[NSString stringWithFormat:@"%lld", ticketID]];
    bAccept = [[dic valueForKey:ACCEPT_TERMS] boolValue];
    if (bAccept)
    {
        accept.image = [UIImage imageNamed:@"check.png"];
    }
    else
    {
        accept.image = [UIImage imageNamed:@"uncheck.png"];
    }
    [signatures addObjectsFromArray:[dic valueForKey:SIGNATURE]];
    
    if ([signatures count] > 0)
    {
        lastSign.image = [signatures lastObject];
        lastSign.hidden = NO;
    }
    
    name.text = [dic valueForKey:SIGN_NAME];
}
- (IBAction)acceptChanged:(id)sender
{
    bAccept ^= TRUE;
    if (bAccept)
    {
        accept.image = [UIImage imageNamed:@"check.png"];
    }
    else
    {
        accept.image = [UIImage imageNamed:@"uncheck.png"];
    }
}
- (IBAction)reject:(id)sender
{
    RejectView* reject = [nibLoader load:@"Reject" params:[NSNumber numberWithLongLong:ticketID]];
    reject.acceptView = self;
    [[[AppDelegate sharedInstance] window] addSubview:reject];
}

- (IBAction)resign:(id)sender
{
    [name resignFirstResponder];
}


-(void)keyboardShow: (NSNotification* )param
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    magicView.center = CGPointMake(512, 330);
    [UIView commitAnimations];
}
-(void)keyboardHide: (NSNotification* )param
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    magicView.center = CGPointMake(512, 330);
    [UIView commitAnimations];
}

@end
